package org.xzx.cloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.xzx.cloud.annotation.IgnoreResponseAdvice;
import org.xzx.cloud.service.PathService;
import org.xzx.cloud.service.PermissionService;
import org.xzx.cloud.vo.CheckPermissionRequest;
import org.xzx.cloud.vo.CreatePathRequest;

import java.util.List;

/**
 * 路径创建与权限校验对外服务接口实现
 */
@Slf4j
@RestController
public class PermissionController {

    private final PathService pathService;

    private final PermissionService permissionService;

    public PermissionController(PathService pathService, PermissionService permissionService) {
        this.pathService = pathService;
        this.permissionService = permissionService;
    }

    /**
     * 路径创建接口
     *
     * @param request {@link CreatePathRequest}
     * @return
     */
    @PostMapping("/create/path")
    public List<Integer> createPath(@RequestBody CreatePathRequest request) {
        log.info("CreatePath:{}", request.getPathInfos().size());
        return pathService.createPath(request);
    }

    /**
     * 权限校验接口
     *
     * @param request
     * @return
     */
    @IgnoreResponseAdvice
    @PostMapping("/check/permission")
    public Boolean checkPermission(@RequestBody CheckPermissionRequest request) {
        log.info("Check Permission For args:{},{},{}", request.getUserId(), request.getUri(), request.getHttpMethod());
        return permissionService.checkPermission(
                request.getUserId(), request.getUri(), request.getHttpMethod()
        );
    }
}
